<template>
<div class="emsLogin">
<div class="emsInput" style="width:400px;text-align:center">
  <el-form :label-position="labelPosition" label-width="100px" style="text-align: center">
    <el-form-item label="员工编号" >
      <el-input v-model="ems.eid" ></el-input>
    </el-form-item>
    <el-form-item label="员工姓名">
      <el-input v-model="ems.name"></el-input>
    </el-form-item>
    <el-form-item label="账号密码">
      <el-input v-model="ems.passwd"></el-input>
    </el-form-item>
    <el-form-item label="柜台号">
      <el-input  v-model="ems.counterId"></el-input>
    </el-form-item>
<el-button type="primary" @click="emsLogin()">登录</el-button>
  </el-form>
</div>

</div>
</template>

<script>
import instance from "@/untils";
import axios from "axios";
export default {
  data() {
    return {
      labelPosition: 'left',
      ems:{}
    }
  },
  methods:{
    emsLogin(){


      axios.post("http://localhost:8888/ems/login",this.ems).then(res=>{
        console.log(res.data);
        let msg=res.data.msg
        if(res.data.status==200){

          localStorage.setItem("key",res.data.data.key);
          localStorage.setItem("ems",res.data.data)
          this.$message({
            message:msg+'  欢迎 '+this.ems.name,
            type:"success"
          });
          console.log(res.data.data.ems.ismanger);
          if(res.data.data.ems.ismanger===0){
            this.$store.commit('logined')
            this.$router.push('/')
          }else if (res.data.data.ems.ismanger===1){

            this.$store.commit('isManger')
            this.$router.push('/mangerHome')
          }

        }
        else {
          this.$message({
            message:msg
          });
        }
      })
    }
  },
  created() {
     let key= localStorage.getItem('key');
     if(key!=null){
       instance.post('/ems/getEmsByToken/'+key).then(res=>{
          if(res.data.status==200){
            this.$store.commit('logined');
            this.$router.push('/')
          }
           }
       )
     }



  }
}
</script>
<style scoped>
.emsInput{
  position: absolute;
  text-align: center;
  top: 25%;
  left: 35%;
}

</style>